package com.quanyan.common.utils;

import com.notnoop.apns.APNS;
import com.notnoop.apns.ApnsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * Created by maoxianliu on 15/3/21.
 */
public class IosPushUtils {
    private static Logger logger = LoggerFactory.getLogger(IosPushUtils.class);

    public static boolean push(String token,String content,Map<String,Object> param){


        String path=IosPushUtils.class.getClassLoader().getResource("").getPath();
        //String filename=path+PropUtils.getProperty("ios.push.cert","/cert/apns-dev-key.p12").trim();
        String filename=path+PropUtils.getProperty("ios.push.cert","/cert/prod_push.p12").trim();
        ApnsService service =APNS.newService().withCert(filename, PropUtils.getProperty("ios.push.password", "fujin123").trim()).
                withProductionDestination().build();
        System.out.printf("token:"+token);
        System.out.printf("content:"+content);


        //String payload;// = APNS.newPayload().alertBody(content).build();
        String payload = APNS.newPayload().customFields(param).sound("default").alertBody(content).build();
        try{
            service.push(token, payload);
            logger.info("payload {}",payload);
            return true;
        }catch(Exception e){
            logger.error("推送IOS信息出错:",e);
            return false;
        }

    }
    public static void main(String args[]){
        System.out.println("this is a test push");
        //push("2939129349912934919234","hello word");
    }
}
